<script type="text/javascript">
	function kaiyao() {
		$("#kaiyaoitem").modal('open');
	}

	var druglist = [];

	function del(val) {
		$("#drugitem" + val + "").remove();
		druglist[val] = undefined;
	}

	function kaiyaosure() {
		var name = $('#drugselect option:selected').text();
		var val = $('#drugselect option:selected').val();
		var num = $("#drugnum").val();
		if (name != "") {
			if (num != "") {
				if (druglist[val] == undefined) {
					if (parseInt($("#ddruginventory" + val + "").find("a")
							.html()) >= parseInt(num)) {
						druglist[val] = num;
						$("#ddrug_info").append(
								"<tr id='drugitem"+val+"'><td>"
										+ name
										+ "</td><td>"
										+ num
										+ "</td><td>"
										+ $("#ddrugprice" + val + "").find("a")
												.html()
										+ "</td><td><button onclick='del("
										+ val + ")'>删除</button></td></tr>");
					} else {
						$("#myalertbody").html("库存不足！");
						$("#my-alert").modal('open');
					}
				} else {
					var nownum = $("#drugitem" + val + "").find("td:eq(1)")
							.html();
					if (parseInt($("#ddruginventory" + val + "").find("a")
							.html()) >= (parseInt(nownum) + parseInt(num))) {
						druglist[val] = parseInt(nownum) + parseInt(num);
						$("#drugitem" + val + "").find("td:eq(1)").html(
								druglist[val]);
					} else {
						$("#myalertbody").html("库存不足！");
						$("#my-alert").modal('open');
					}
				}
			} else {
				$("#myalertbody").html("请输入药品数量！");
				$("#my-alert").modal('open');
			}
		} else {
			$("#myalertbody").html("请选中药品！");
			$("#my-alert").modal('open');
		}
	}

	function kaiyaofinish() {
		if($("#doccid").val()!=""&&$("#pattid").val()!=""){
		var drugl = [];
		$("#ddrug_info").find("tr").each(function(i) {
			var obj = new Object();
			obj.name = $(this).find("td:eq(0)").html();
			obj.num = $(this).find("td:eq(1)").html();
			obj.docid = $("#doccid").val();
			obj.patient = $("#pattid").val();
			drugl[i] = obj;
		});
		$.post('${ctx}/main/kaiyao', {
			"druglist" : JSON.stringify(drugl)
		}, function(data) {
			if (data.success.success) {
				$("#myalertbody").html("开药成功！");
				$("#kaiyaoitem").modal('close');
				$("#my-alert").modal('open');
				$("#ddrug_info").html("");
				$("#doccid").val("");
				$("#pattid").val("");
				$("#drugnum").val("");
			}else{
				$("#myalertbody").html(data.success.fail);
				$("#kaiyaoitem").modal('close');
			}
		}, 'json');
		}
		else{
			$("#myalertbody").html("信息不得为空！");
			$("#my-alert").modal('open');
		}
	}
</script>

<div class="am-modal am-modal-no-btn" tabindex="-1" id="kaiyaoitem">
	<div class="am-modal-dialog" style="border-radius: 10px;width:600px">
		<div class="am-modal-hd">
			开药列表 <a href="javascript: void(0)" class="am-close am-close-spin"
				data-am-modal-close>&times;</a>
		</div>
		<div class="am-modal-bd">
			<div>
				<table border="0" class="am-table am-table-radius am-table-striped">
					<tr>
						<td><input placeholder="病人序列号" type="text" id="pattid" /></td>
						<td><input placeholder="医生ID" type="text" id="doccid" /></td>
						<td></td>
					</tr>
					<tr>
						<td><select id="drugselect" style="width:214px">
								<option></option> <#if drug ??> <#list drug as d>
								<option value="${d_index}">${d.drug_name}</option> </#list>
								</#if>
						</select></td>
						<td><input placeholder="药品数量" type="text" id="drugnum" /></td>
						<td><button onclick="kaiyaosure()" >确定</button></td>
					</tr>
				</table>

			</div>
			<div id="paitentinfo1" style="display:">
				<table
					class="am-table am-table-bordered am-table-radius am-table-striped">
					<thead>
						<tr>
							<th style="text-align: center">药名</th>
							<th style="text-align: center">数量</th>
							<th style="text-align: center">价格</th>
							<th style="text-align: center">操作</th>
						</tr>
					</thead>
					<tbody id="ddrug_info">

					</tbody>
				</table>
			</div>
			<button onclick="kaiyaofinish()" style="float: right;margin-bottom:10px">提交药方</button>
		</div>
	</div>
</div>